package com.jason.learn.springcloud.orderservice.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static ThreadLocal<SimpleDateFormat> smf = new ThreadLocal<>();
    static{
        smf.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static Date getDateTime(Long date){
        return new Date(date);
    }

    public static int getYearAndMonth(Long date) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        int month = c.get(Calendar.MONTH);
        int year = (c.get(Calendar.YEAR)-2000)*100;
        return year+month+1;
    }

    public static int getDay(Long date) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        return c.get(Calendar.DATE);
    }

    public static Long getDateTime(Date date){
        return date.getTime();
    }

    public static Long getDateTimeLessTenHour(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR,-10);
        return c.getTime().getTime();
    }

    public static String getDateTimeStr(Long date){
        return smf.get().format(getDateTime(date));
    }

    public static String getDateTimeStr(Date date){
        return smf.get().format(getDateTime(date));
    }

    public static boolean isBefore3Days(Long dateTime){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(dateTime);

        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);
        today.add(Calendar.DATE,-3);

        return c.before(today);
    }
}
